package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.PrinterException;
import java.time.LocalDateTime;
import java.util.List;

import static com.zzyl.enums.BasicEnum.BED_DELETEID_FAIL;
import static com.zzyl.enums.BasicEnum.BED_UPDATE_FAIL;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }


    //新增床位
    @Override
    public int addBed(BedDto bedDto) {
        //创建一个类型全的类
        Bed bed = new Bed();
        //将bedDto拷贝Bed里
        BeanUtils.copyProperties(bedDto, bed);
        //设置bed里的值
        //设置时间
        LocalDateTime now = LocalDateTime.now();
        bed.setCreateTime(now);
        bed.setUpdateTime(now);
        //设置修改人
        bed.setCreateBy(1L);
        bed.setUpdateBy(1L);
        //设置床位状态
        bed.setBedStatus(0);
        //调用bedMapper
        int i = bedMapper.insertBed(bed);
        return i;
    }

    //根据id查询
    @Override
    public BedVo selectById(Long id) {
        //传入数据
        Bed bed = bedMapper.selectById(id);
        BedVo bedVo = new BedVo();
        BeanUtils.copyProperties(bed, bedVo);
        return bedVo;
    }

    //修改床位信息
    @Override
    public int updateBed(BedDto bedDto) {
        if (bedDto == null) {

            throw new BaseException(BED_UPDATE_FAIL);
        }
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        LocalDateTime now = LocalDateTime.now();
        bed.setUpdateTime(now);
        //TODO:登陆完成后获取修改人id
        bed.setCreateBy(1L);
        int i = bedMapper.updateBed(bed);
        return i;
    }

    //根据id删除床位
    @Override
    public int deleteById(Long id) {
        //调用根据id查询方法查询该床位状态码是不是在使用状态,在使用状态不能删除
        Bed bed = bedMapper.selectById(id);
        if (bed.getBedStatus() != 0) {
            //抛一个全局异常出去
            throw new BaseException(BED_DELETEID_FAIL);
        }
        int i = bedMapper.deleteById(id);
        return i;
    }

}

